/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.accessorgen;

import com.inspur.edp.caf.generator.item.ClassGeneratorContext;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorCopySelfGen;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorCreateAndSetChildCollectionGen;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorInitializeChildCollectionGen;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorInitializeNestedGen;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorInnerChangedGen;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AcsCreateChildGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.EntityAccessorCtorGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.EntityAccessorInitializeGen;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.GetChildsGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.NewChildCollectionGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.base.AccessorInitializeGen;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.base.AcsCopyGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.entity.EntityAcsCopyGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.AccessorChildCollectionProperty;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.NodeCodeGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.BaseDataGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property.ChildPropGenerator;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.Modifier;

public abstract class EntityAcsGenerator extends BaseAcsGenerator {
	public AccessorClassGenContext getAccClassGenContext() {
		return (AccessorClassGenContext) super.getContext();
	}

	public abstract AccessorClassGenContext getRootContext();

	protected EntityAcsGenerator(EntityGeneratorContext parContext,
			 String nodeCode, IGspCommonDataType dataType, MediateType targetInterfaceType) {
		super(parContext,dataType, nodeCode, targetInterfaceType);
	}

	@Override
	protected ClassGeneratorContext createClassInfoContext() {
		return new AccessorClassGenContext(ParentContext);
	}

	@Override
	protected void addExtendPropertyGenerator(java.util.ArrayList<ClassPropertyGenerator> proGenerators) {
		super.addExtendPropertyGenerator(proGenerators);
		NodeCodeGenerator gen = getNodeCodeGenerator(NodeCode);
		proGenerators.add(gen);
	}

	@Override
	protected java.util.ArrayList<ClassMethodGenerator> createMethodGenerators() {
		GetChildsGenerator getChildGen = getChildsMedGenerator(PropertyInfos);

		java.util.ArrayList<ClassMethodGenerator> methodGenerators = super.createMethodGenerators();
		if (methodGenerators == null) {
			methodGenerators = new java.util.ArrayList<ClassMethodGenerator>();
		}
		methodGenerators.add(getChildGen);

		ClassMethodGenerator childInitGen = get_CreateAndSetChildCollection_Generator();
		methodGenerators.add(childInitGen);

//		ClassMethodGenerator acceptChangeGen = get_AcceptChange_Generator();
//		methodGenerators.add(acceptChangeGen);
		return methodGenerators;
	}

	@Override
	protected java.util.ArrayList<ClassMethodGenerator> createConstructorGenerators() {
		java.util.ArrayList<ClassMethodGenerator> list = super.createConstructorGenerators();
		if (list == null)
			list = new java.util.ArrayList<ClassMethodGenerator>();
		list.add(new EntityAccessorCtorGenerator(getName(), TargetInterfaceType));
		return list;
	}

	protected java.util.Map<String, String> ChildFieldsMappings = new java.util.HashMap<String, String>();

	@Override
	protected ChildPropGenerator getChildPropGenerator(String childCode,
			MediatePropertyInfo propertyInfo, String privatePropName) {
		ChildFieldsMappings.put(propertyInfo.getNode().getCode(), privatePropName);
		return new AccessorChildCollectionProperty(ParentContext, propertyInfo.getNode().getCode(), propertyInfo, privatePropName);
	}


	private NodeCodeGenerator getNodeCodeGenerator(String nodeCode) {
		ArrayList<Modifier.ModifierKeyword> modifierKeywords = new ArrayList<>();
		modifierKeywords.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);

		return new NodeCodeGenerator(nodeCode, modifierKeywords, true);
	}

	private GetChildsGenerator getChildsMedGenerator(List<MediatePropertyInfo> propertyInfos) {
		ArrayList<Modifier.ModifierKeyword> modifierKeywords = new ArrayList<>();
		modifierKeywords.add(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
		return new GetChildsGenerator(Node, modifierKeywords);
	}

	private ClassMethodGenerator get_CreateAndSetChildCollection_Generator() {
		return new AccessorCreateAndSetChildCollectionGen((IGspCommonObject) Node, ChildFieldsMappings, getRootContext().getAllTypeInfo());
	}


	@Override
	public AccessorInitializeGen getInitilizeGenerator() {
		return new EntityAccessorInitializeGen( ChildFieldsMappings, getRootContext().getAllTypeInfo());
	}

	@Override
	public AcsCopyGenerator getRestGenerator() {
		return new EntityAcsCopyGenerator(getName(),NodeCode, PropertyInfos, ChildFieldsMappings,ParentContext);
	}

	protected abstract ClassMethodGenerator get_AcceptChange_Generator();

	@Override
	protected BaseDataGenerator getChildsGenerator(EntityGeneratorContext parContext,
			IGspCommonObject node) {

		MediateType interfaceType = parContext.getProjContext().getCefGenContext()
				.getEntityGenContext().getNodeTypes().get(node.getCode());
		return new AccessorChildClassGenerator(parContext, getRootContext(), node, interfaceType,
				TargetInterfaceType);
	}

	@Override
	protected void addAcsExtendMethod(java.util.ArrayList<ClassMethodGenerator> methodGens) {
		AcsCreateChildGenerator createChildGen = new AcsCreateChildGenerator(Node, ChildTypes, getRootContext());
		methodGens.add(createChildGen);
		methodGens.add(new AccessorCopySelfGen(getAccClassGenContext(), TargetInterfaceType,Node, getRootContext().getAllTypeInfo()));
		methodGens.add(new AccessorInitializeNestedGen(getAccClassGenContext(),NodeCode, PropertyInfos, TargetInterfaceType, getRootContext().getAllTypeInfo(), false));
		methodGens.add(new AccessorInitializeChildCollectionGen(TargetInterfaceType,Node, ChildFieldsMappings, getRootContext().getAllTypeInfo()));
		methodGens.add(new AccessorInnerChangedGen());
		for (IGspCommonObject childNode : ((IGspCommonObject) Node).getContainChildObjects()) {
			methodGens.add(new NewChildCollectionGenerator(ParentContext, childNode, false));
		}
	}
}
